package com.example.demo.service.bis.impl;

import com.example.demo.bean.sys.SysDict;
import com.example.demo.bean.bis.Appeal;
import com.example.demo.bean.bis.Picture;
import com.example.demo.bean.bis.Sign;
import com.example.demo.repository.AppealMapper;
import com.example.demo.repository.PictureMapper;
import com.example.demo.repository.SignMapper;
import com.example.demo.repository.SysDictMapper;
import com.example.demo.service.bis.AppealService;
import com.example.demo.service.exception.ErrorCode;
import com.example.demo.service.exception.ServiceException;
import com.example.demo.utils.ConstantUtils;
import com.example.demo.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author zzz
 * @Title: AppealServiceImpl
 * @ProjectName test
 * @Description: TODO(申诉)
 * @date 2018/7/20
 */
@Service
@Transactional
public class AppealServiceImpl implements AppealService {

    @Autowired private AppealMapper appealMapper;

    @Autowired private SignMapper  signMapper;

    @Autowired private PictureMapper pictureMapper;

    @Autowired private SysDictMapper  sysDictMapper;

    @Value("${IMG.URL.MIN.SUFIX}")
    private String imgUrl;

    @Override
    public Boolean insert(Appeal appeal) {
        appeal.setState(ConstantUtils.INTEGER_TWENTY_SEVEN);
        appeal.setTime(DateUtils.getTime(null));
        List<SysDict> list = sysDictMapper.findById(9);
        if(list.size()>0){
            for(int i=0 ;i<list.size();i++){
                if(list.get(i).getName().equals(appeal.getTypeName())){
                    appeal.setType(list.get(i).getId());
                }
            }

        }
        if(appealMapper.insert(appeal)>0){
            return true;
        }
        return false;
    }

    @Override
    public List<Appeal> findByDeptId(Integer id) {
        if( StringUtils.isEmpty(id) ){
            throw  new ServiceException(ErrorCode.NULL_ERROR);
        }
        return appealMapper.selectByPrimaryUid(id);
    }

    @Override
    public Boolean update(Integer id, Integer state) {
        if(StringUtils.isEmpty(id) ){
            throw  new ServiceException(ErrorCode.NULL_ERROR);
        }
        Appeal appeal = new Appeal();
        Sign sign = new Sign();
        appeal = appealMapper.selectByPrimaryId(id);
        if (!StringUtils.isEmpty(appeal)){
            appeal.setId(id);
            appeal.setApprovalTime(DateUtils.getTime(null));
            appeal.setState(state);
            if(state==ConstantUtils.INTEGER_TWENTY_SEVEN){
                sign =  signMapper.selectByPrimaryKey(appeal.getUId(),appeal.getSignTime().split("&")[0]);
                if(!StringUtils.isEmpty(sign)){
                    if (appeal.getType() == ConstantUtils.INTEGER_ELEVEN){
                        //质疑早上
                        if(sign.getState().equals(ConstantUtils.INTEGER_FOUR)){
                            sign.setState(ConstantUtils.INTEGER_TWO.toString());
                        }else if(sign.getState().equals(ConstantUtils.INTEGER_EIGHT)){
                            sign.setState(ConstantUtils.STRING_THREE);
                        }
                    }else{
                        //质疑下午
                        if(sign.getState().equals(ConstantUtils.INTEGER_THREE)){
                            sign.setState(ConstantUtils.INTEGER_TWO.toString());
                        }else if(sign.getState().equals(ConstantUtils.INTEGER_EIGHT)){
                            sign.setState(ConstantUtils.STRING_FOUR);
                        }
                    }
                }
            }
            if(appealMapper.updateByPrimaryKey(appeal)>0
                    && signMapper.updateByPrimaryKey(sign)>0){
                return true;
            }
        }
        return false;
    }

    @Override
    public List<Picture> selectAll(String startTime, String endTime) {
       // PageHelper.startPage(page, pageSize);
        List<Picture> list = pictureMapper.selectAll(startTime,endTime);
        //PageInfo<Picture> pages = new PageInfo<>(list);
        for(int i=0;i<list.size();i++){
            list.get(i).setPictureMin(imgUrl+list.get(i).getPictureMin());
        }
        return list;
    }


}
